package com.company.utils;

import java.io.FileNotFoundException;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.util.ArrayList;  
import java.util.Date;  
import java.util.HashMap;  
import java.util.List;  
import java.util.Map;  
  
import org.apache.poi.ss.usermodel.Workbook;  
import org.apache.poi.xssf.usermodel.XSSFWorkbook;  
  
public class TestCreat {  
  
    /** 
     * @param args 
     */  
public static void main(String[] args){  
        //  模拟数据  
        List<Map<String,Object>> datas = new ArrayList<Map<String,Object>>();  
          
        Map<String,Object> map = new HashMap<String,Object>();  
          
        map.put("用户名", "xiaomaha");  
        map.put("密码", "1234567qq");  
        map.put("创建日期", new Date());  
          
        Map<String,Object> map2 = new HashMap<String,Object>();  
          
        map2.put("用户名", "xiaomaha21111111111111111111111111111111111111中文11111112");  
        map2.put("密码", "1-1234567qq11111111111111111111111111111111111111中文111111111113");  
        map2.put("创建日期", new Date());  
          
        Map<String,Object> map3 = new HashMap<String,Object>();  
        map3.put("用户名", "xiaomaha3");  
        map3.put("密码", "2-1234567qq");  
        map3.put("创建日期", new Date());  
          
        datas.add(map);  
        datas.add(map2);  
        datas.add(map3);  
        for(int i=0;i<500;i++){  
            Map<String,Object> maps = new HashMap<String,Object>();  
            maps.put("用户名", "xiaomaha3"+i);  
            maps.put("密码", "2-1234567qq"+i);  
            maps.put("创建日期", new Date());  
            datas.add(maps);  
        }  
          
        //创建excel  
        ExcelCreater et = new ExcelCreaterImpl(datas);  
        et.setSheetName("测试");  
        Map<String, String> mess = new HashMap<String, String>();  
        mess.put("用户名","userName");  
        mess.put("密码","pw");  
        mess.put("创建日期","createDate");  
        et.setMess(mess);  
        et.isAutoSizeColumn(true);  
        //      同时生成2003、2007版  
//      Map<String, Workbook> workMap = et.createExcel();  
//      Workbook wb = workMap.get(ExcelTemplate.EXCEL_07); //获取excel2007  
        Workbook wb = et.createExcel(ExcelCreater.EXCEL_03);         
          
        //如果愿意英汉转换  
  
          
//      1:最好的方法是写一个xml！ 然后解析  
//      2:json格式，当然需要相对应的json插件  
//      3:Map实现  
//      ---------------------------------------  
//      Map例子  
//      Map<String,Object> map3 = new HashMap<String,Object>();  
//      map3.put("userName", "xiaomaha3");  
//      map3.put("passWord", "2-1234567qq");  
//      map3.put("createDate", new Date());  
//      Map<String,String> m = new HashMap<String,String>();  
//      m.put("userName", "用户名");  
//      m.put("passWord", "密码");  
//      m.put("createDate", "创建日期");  
//      然后ExcelTemplate 加入一个方法就可以实现了  
          
          
          
        //测试  写入磁盘  
        String filename = "d://workbook2.xls";  
        if (wb instanceof XSSFWorkbook) {  
            filename = filename + "x";  
        }  
          
          
        FileOutputStream out;  
        try {  
            out = new FileOutputStream(filename);  
              
            try {  
                wb.write(out);  
                out.close();  
            } catch (IOException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
              
              
        } catch (FileNotFoundException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
          
          
    }  
  
}  